﻿namespace USElections.Newsfeed.Pages
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Media;
    using AgFx;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using NorthernLights;
    using USElections.NewsFeeds.Model;

    /// <summary>
    /// NewsFeedsPage class.
    /// </summary>
    public partial class NewsFeedsPage : PhoneApplicationPage
    {
        #region fields
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the NewsFeedsPage class.
        /// </summary>
        public NewsFeedsPage()
        {
            InitializeComponent();

            this.DataContext = NewsFeeds.Model.Data.Instance.RssFeeds;

            Utils.General.RegisterForNotification(this, "IsLoading", DataManager.Current, (d, e) =>
            {
                if (SystemTray.ProgressIndicator != null)
                {
                    SystemTray.ProgressIndicator.IsVisible = DataManager.Current.IsLoading;
                }
            });
        }
        #endregion

        #region Public methods
        /// <summary>
        /// On Navigated To.
        /// </summary>
        /// <param name="e">The NavigationEventArgs object.</param>
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
        }
        #endregion

        #region Private methods
        private void listBoxTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ListBox listBox = sender as ListBox;

            if (listBox != null && listBox.SelectedItem != null)
            {
                int feed = this.pivot.SelectedIndex;
                int item = listBox.SelectedIndex;

                StateVariables.Set<int>("Feed", feed);
                StateVariables.Set<int>("FeedItem", item);

                // set state, to retrieve in feedreaderpage..
                this.NavigationService.Navigate(new Uri("/USElections.Newsfeed;component/Pages/FeedReaderPage.xaml", UriKind.Relative));
            }
        }

        private void ApplicationBarIconButtonClick(object sender, EventArgs e)
        {
            ApplicationBarIconButton item = sender as ApplicationBarIconButton;

            if (item != null)
            {
                // refresh feed.
                if (string.Compare(item.Text, "refresh", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    RssFeedModel feed = this.pivot.SelectedItem as RssFeedModel;

                    if (feed != null)
                    {
                        feed = DataManager.Current.Refresh<RssFeedModel>(
                            new RssFeedLoadContext(feed.Name, feed.Uri),
                            (vm) =>
                            {
                                // do nothing
                            },
                            (vm) =>
                            {
                                string message = "An error occurred while loading the feed, please try again later";
                                this.ErrorLoadingFeed(message);
                            });
                    }
                }
            }
        }

        private void ApplicationBarMenuItemClick(object sender, EventArgs e)
        {
            // refresh all the feeds.
            for (int i = 0; i < this.pivot.Items.Count; i++)
            {
                RssFeedModel feed = this.pivot.Items[i] as RssFeedModel;

                if (feed != null)
                {
                    feed = DataManager.Current.Refresh<RssFeedModel>(
                        new RssFeedLoadContext(feed.Name, feed.Uri),
                        (vm) =>
                        {
                            // do nothing
                        },
                        (vm) =>
                        {
                            string message = "An error occurred while loading one of the feeds, please try again later";
                            this.ErrorLoadingFeed(message);
                        });
                }
            }
        }

        private void ErrorLoadingFeed(string message)
        {
            this.notification.Show(message, new SolidColorBrush(Colors.Red), null);
        }
        #endregion
    }
}